#include "link_graph.h"

LinkGraph* create_lgp(const char* str)
{
    LinkGraph* graph = malloc(sizeof(LinkGraph));
    graph->vexnum = strlen(str);
    graph->vertex = malloc(sizeof(Vertex) * graph->vexnum);
    for(int i = 0; i<graph->vexnum; i++)
    {
        graph->vertex[i].vex = str[i];
        graph->vertex[i].head = NULL;
    }
    return graph;
}

int _get_vex_lgp(LinkGraph* graph, char v)
{
    for(int i = 0; i < graph->vexnum; i++)
    {
        if(graph->vertex[i].vex == v)
            return i;
    }
    return -1;
}

Edge* create_eg_lgp(int index)
{
    Edge* edge = malloc(sizeof(Edge));
    edge->index = index;
    edge->next = NULL;
    return edge;
}

bool add_eg_lgp(LinkGraph* graph, char v1, char v2)
{
    if(graph->vexnum < 2)
        return false;
    int t1 = _get_vex_lgp(graph,v1),t2 = _get_vex_lgp(graph,v2);
    if(-1 == t1 || -1 == t2)
        return false;
    Edge* e = create_eg_lgp(t2);
    e->next = graph->vertex[t1].head;
    graph->vertex[t1].head = e;
    return true;
}

void traversal_lgp(LinkGraph* graph)
{
    for(int i = 0; i< graph->vexnum; i++)
    {
        printf("%d %c:",i,graph->vertex[i].vex);
        for(Edge* e = graph->vertex[i].head; NULL != e; e = e->next)
        {
            printf("%c ",graph->vertex[e->index].vex);
        }
        printf("\n");
    }
}

size_t vex_out_lgp(LinkGraph* graph,char v)
{
    int t = _get_vex_lgp(graph,v);
    if(t == -1)
        return 0;
    size_t cnt = 0;
    for(Edge* e = graph->vertex[t].head; NULL != e; e = e->next)
    {
        cnt++;
    }
    return cnt;
}

size_t vex_in_lgp(LinkGraph* graph,char v)
{
    int t = _get_vex_lgp(graph,v);
    if(-1 == t)
        return 0;
    size_t cnt = 0;
    for(int i = 0; i<graph->vexnum; i++)
    {
        if(i == t)
            continue;
        for(Edge* e = graph->vertex[i].head; NULL != e; e = e->next)
        {
            if(e->index == t)
            {
                cnt++;
                break;
            }
        }
    }
    return cnt;
}

void bfs_lgp(LinkGraph* graph)
{

}
